/*
 * Created by JFormDesigner on Sat Sep 30 17:41:02 CST 2023
 */

package io.xccit.frame;

import io.xccit.config.LoginUserConfig;
import io.xccit.pojo.User;
import io.xccit.service.UserService;
import io.xccit.service.impl.UserServiceImpl;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author China_ywx
 */
public class RegisterFrame extends JFrame {

    private UserService userService = new UserServiceImpl();
    public RegisterFrame() {
        initComponents();
    }

    /**
     * 注册按钮点击事件
     * @param e
     */
    private void register_btnMouseClicked(MouseEvent e) {
        String username = username_input.getText().toString();
        String password = String.valueOf(password_input.getPassword());
        String nickname = nickname_input.getText().toString();
        User user = new User(username, password, nickname);
//        System.out.println(user);
        User checkUser = userService.checkUser(user);
        if (checkUser != null){
            JOptionPane.showMessageDialog(null,"账号已被注册,请重新输入");
        }else {
            int rows = userService.register(user);
            if (rows > 0){
                JOptionPane.showMessageDialog(null,"注册成功");
                LoginUserConfig.setUsername(user.getUsername());
                LoginUserConfig.setPassword(user.getPassword());
                LoginUserConfig.setNickName(user.getNickname());
                dispose();
                LoginFrame loginFrame = new LoginFrame();
                loginFrame.setVisible(true);
                loginFrame.setValue(LoginUserConfig.getUsername(),LoginUserConfig.getPassword());
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        title = new JLabel();
        username_label = new JLabel();
        username_input = new JTextField();
        password_label = new JLabel();
        password_input = new JPasswordField();
        register_btn = new JButton();
        login_img = new JLabel();
        nickname_label = new JLabel();
        nickname_input = new JTextField();

        //======== this ========
        setBackground(new Color(0x66ffcc));
        setTitle("\u5b66\u751f\u8ba2\u9910\u7cfb\u7edf-\u6ce8\u518c");
        setIconImage(new ImageIcon(getClass().getResource("/images/globalIcon.png")).getImage());
        setResizable(false);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- title ----
        title.setText("\u5b66\u751f\u7f51\u4e0a\u8ba2\u9910\u7cfb\u7edf-\u6ce8\u518c");
        title.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));
        contentPane.add(title);
        title.setBounds(400, 85, 230, 30);

        //---- username_label ----
        username_label.setText("username:");
        username_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        contentPane.add(username_label);
        username_label.setBounds(305, 170, 70, 20);

        //---- username_input ----
        username_input.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(username_input);
        username_input.setBounds(390, 165, 260, 30);

        //---- password_label ----
        password_label.setText("password:");
        password_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        contentPane.add(password_label);
        password_label.setBounds(305, 235, 70, 20);
        contentPane.add(password_input);
        password_input.setBounds(390, 230, 260, 30);

        //---- register_btn ----
        register_btn.setText("\u6ce8 \u518c");
        register_btn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        register_btn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                register_btnMouseClicked(e);
            }
        });
        contentPane.add(register_btn);
        register_btn.setBounds(570, 330, 73, 25);

        //---- login_img ----
        login_img.setIcon(new ImageIcon(getClass().getResource("/images/login_img.png")));
        login_img.setHorizontalAlignment(SwingConstants.CENTER);
        login_img.setToolTipText("\u6b66\u5a01\u804c\u4e1a\u5b66\u9662\u6821\u56ed\u8ba2\u9910");
        contentPane.add(login_img);
        login_img.setBounds(0, 0, 296, 458);

        //---- nickname_label ----
        nickname_label.setText("nickname:");
        nickname_label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        contentPane.add(nickname_label);
        nickname_label.setBounds(305, 295, 70, 20);

        //---- nickname_input ----
        nickname_input.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(nickname_input);
        nickname_input.setBounds(390, 290, 260, 30);

        contentPane.setPreferredSize(new Dimension(705, 490));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel title;
    private JLabel username_label;
    private JTextField username_input;
    private JLabel password_label;
    private JPasswordField password_input;
    private JButton register_btn;
    private JLabel login_img;
    private JLabel nickname_label;
    private JTextField nickname_input;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
